home *** CD-ROM | disk | FTP | other *** search
/ Linux Cubed Series 7: Sunsite / Linux Cubed Series 7 - Sunsite Vol 1.iso / system / news / inn1.000 / inn1.4sec-linux-src.tar / inn / frontends / feedone.c < prev    next >
C/C++ Source or Header  |  1993-01-29  |  4KB  |  209 lines

  1. /*  $Revision: 1.13 $
  2. **
  3. **  Connect to the NNTP server and feed one article.
  4. */
  5. #include "configdata.h"
  6. #include <stdio.h>
  7. #include <errno.h>
  8. #include <sys/types.h>
  9. #include "nntp.h"
  10. #include "libinn.h"
  11. #include "clibrary.h"
  12. #include "macros.h"
  13.  
  14.  
  15. STATIC FILE    *FromServer;
  16. STATIC FILE    *ToServer;
  17. STATIC int    Tracing;
  18.  
  19.  
  20. /*
  21. **  Print and error message (with errno) and exit with an error code.
  22. */
  23. STATIC NORETURN
  24. PerrorExit(s)
  25.     char    *s;
  26. {
  27.     (void)fprintf(stderr, "%s, %s.\n", s, strerror(errno));
  28.     exit(1);
  29. }
  30.  
  31.  
  32. /*
  33. **  Read a line from the server or die trying.
  34. */
  35. STATIC NORETURN
  36. GetFromServer(buff, size, text)
  37.     char    *buff;
  38.     int        size;
  39.     char    *text;
  40. {
  41.     if (fgets(buff, size, FromServer) == NULL)
  42.     PerrorExit(text);
  43.     if (Tracing)
  44.     printf("S: %s", buff);
  45. }
  46.  
  47.  
  48. /*
  49. **  Flush a stdio FILE; exit if there are any errors.
  50. */
  51. STATIC void
  52. SafeFlush(F)
  53.     FILE    *F;
  54. {
  55.     if (fflush(F) == EOF || ferror(F))
  56.     PerrorExit("Can't send text to server");
  57. }
  58.  
  59.  
  60. STATIC NORETURN
  61. SendQuit(x)
  62.     int        x;
  63. {
  64.     char    buff[BUFSIZ];
  65.  
  66.     /* Close up. */
  67.     (void)fprintf(ToServer, "quit\r\n");
  68.     SafeFlush(ToServer);
  69.     (void)fclose(ToServer);
  70.     GetFromServer(buff, sizeof buff, "Can't get reply to quit");
  71.     exit(x);
  72. }
  73.  
  74.  
  75. STATIC NORETURN
  76. Usage()
  77. {
  78.     (void)fprintf(stderr,
  79.         "Usage: feedone [-r|-m msgid] [-p] [-t] articlefile\n");
  80.     exit(1);
  81. }
  82.  
  83.  
  84. int
  85. main(ac, av)
  86.     int        ac;
  87.     char    *av[];
  88. {
  89.     static char    MESGIDHDR[] = "Message-ID:";
  90.     int        i;
  91.     FILE    *F;
  92.     char    buff[BUFSIZ];
  93.     char    mesgid[SMBUF];
  94.     char    *p;
  95.     char    *q;
  96.     BOOL    PostMode;
  97.  
  98.     /* Set defaults. */
  99.     mesgid[0] = '\0';
  100.     PostMode = FALSE;
  101.  
  102.     /* Parse JCL. */
  103.     while ((i = getopt(ac, av, "m:prt")) != EOF)
  104.     switch (i) {
  105.     default:
  106.         Usage();
  107.         /* NOTREACHED */
  108.     case 'm':            /* Specified Message-ID */
  109.         if (*optarg == '<')
  110.         (void)strcpy(mesgid, optarg);
  111.         else
  112.         (void)sprintf(mesgid, "<%s>", optarg);
  113.         break;
  114.     case 'p':            /* Use Post, not ihave    */
  115.         PostMode = TRUE;
  116.         break;
  117.     case 'r':            /* Random Message-ID    */
  118.         (void)sprintf(mesgid, "<%d@%ld>",
  119.             getpid(), (long)time((time_t *)NULL));
  120.         break;
  121.     case 't':
  122.         Tracing = TRUE;
  123.         break;
  124.     }
  125.     ac -= optind;
  126.     av += optind;
  127.  
  128.     /* One argument; the input filename. */
  129.     if (ac != 1)
  130.     Usage();
  131.     if ((F = fopen(av[0], "r")) == NULL)
  132.     PerrorExit("Can't open input");
  133.  
  134.     /* Scan for the message-id. */
  135.     if (mesgid[0] == '\0') {
  136.     while (fgets(buff, sizeof buff, F) != NULL)
  137.         if (caseEQn(buff, MESGIDHDR, STRLEN(MESGIDHDR))) {
  138.         if ((p = strchr(buff, '<')) == NULL
  139.          || (q = strchr(p, '>')) == NULL) {
  140.             (void)fprintf(stderr, "Bad mesgid line.\n");
  141.             exit(1);
  142.         }
  143.         q[1] = '\0';
  144.         (void)strcpy(mesgid, p);
  145.         break;
  146.         }
  147.     if (mesgid[0] == '\0') {
  148.         (void)fprintf(stderr, "No Message-ID.\n");
  149.         exit(1);
  150.     }
  151.     }
  152.  
  153.     /* Connect to the server. */
  154.     if (NNTPremoteopen(&FromServer, &ToServer, buff) < 0
  155.      || FromServer == NULL
  156.      || ToServer == NULL) {
  157.     if (buff[0])
  158.         (void)fprintf(stderr, "Server says: %s\n", buff);
  159.     PerrorExit("Can't connect to server");
  160.     }
  161.  
  162.     /* Does the server want this article? */
  163.     if (PostMode) {
  164.     (void)fprintf(ToServer, "post\r\n");
  165.     i = NNTP_START_POST_VAL;
  166.     }
  167.     else {
  168.     (void)fprintf(ToServer, "ihave %s\r\n", mesgid);
  169.     i = NNTP_SENDIT_VAL;
  170.     }
  171.     SafeFlush(ToServer);
  172.     GetFromServer(buff, sizeof buff, "Can't offer article to server");
  173.     if (atoi(buff) != i) {
  174.     (void)fprintf(stderr, "Server doesn't want the article:\n\t%s\n",
  175.         buff);
  176.     SendQuit(1);
  177.     }
  178.  
  179.     /* Send the file over. */
  180.     (void)fseek(F, (OFFSET_T)0, SEEK_SET);
  181.     while (fgets(buff, sizeof buff, F) != NULL) {
  182.     if (caseEQn(buff, MESGIDHDR, STRLEN(MESGIDHDR))) {
  183.         (void)fprintf(ToServer, "%s %s\r\n", MESGIDHDR, mesgid);
  184.         continue;
  185.     }
  186.     if ((p = strchr(buff, '\n')) != NULL)
  187.         *p = '\0';
  188.     (void)fprintf(ToServer, buff[0] == '.' ? ".%s\r\n" : "%s\r\n",
  189.         buff);
  190.     SafeFlush(ToServer);
  191.     }
  192.     (void)fprintf(ToServer, ".\r\n");
  193.     SafeFlush(ToServer);
  194.     (void)fclose(F);
  195.  
  196.     /* How did the server respond? */
  197.     GetFromServer(buff, sizeof buff,
  198.     "No reply from server after sending the article");
  199.     i = PostMode ? NNTP_POSTEDOK_VAL : NNTP_TOOKIT_VAL;
  200.     if (atoi(buff) != i) {
  201.     (void)fprintf(stderr, "Can't send article to the server:\n\t%s\n",
  202.         buff);
  203.     exit(1);
  204.     }
  205.  
  206.     SendQuit(0);
  207.     /* NOTREACHED */
  208. }
  209.